using System;
using System.Collections.Generic;
using System.Text;
using DevExpress.Xpo;
using Component;
namespace Component
{
    public class CategoryDB : Base
    {
        /// <summary>
        /// public static XPCollection<Category> GetAll()
        /// </summary>
        /// <returns>XPCollection<Category></returns>
        public static XPCollection<Category> GetAll(bool admin)
        {
            XPCollection<Category> _xpCategory = new XPCollection<Category>(KetNoiCSDL.session);
            _xpCategory.Filter = FilterAll(admin);
            _xpCategory.Sorting = SetupSort(ColumCategory._Priority,true);
            return _xpCategory;
        }
        /// <summary>
        /// public static Category GetObjectByid(int id)
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static Category GetObjectByid(int id,bool admin)
        {
            XPCollection<Category> _xpCategory = new XPCollection<Category>(KetNoiCSDL.session);
            _xpCategory.Filter = FilterById(id, admin);
            try
            {
                return _xpCategory[0];
            }
            catch
            { return null; }
        }
        /// <summary>
        ///    public static bool TestExistName(string columName, string name,bool admin)
        /// </summary>
        /// <param name="columName"></param>
        /// <param name="name"></param>
        /// <param name="admin"></param>
        /// <returns></returns>
        public static bool TestExistName(string columName, string name,bool admin)
        {
            XPCollection<Category> _xpCategory = new XPCollection<Category>(KetNoiCSDL.session);
            _xpCategory.Filter = FilterByString(ColumCategory._Name,name, admin);
            if (_xpCategory.Count > 0)
            {
                return true;
            }
            return false;

        }
        /// <summary>
        ///   public static string GetContentByLanguage(string columName, Category cat)
        /// </summary>
        /// <param name="columName"></param>
        /// <param name="cat"></param>
        /// <returns></returns>
        public static string GetContentByLanguage(string columName, Category cat)
        {
            switch (columName)
            {
                case ColumCategory._Name:
                    {
                        if (LanguageVN == true)
                            return cat.Name;
                        return cat.NameEL;
                    }
            }
            return cat.Name;
        }
        /// <summary>
        ///    public static XPCollection<Category> GetAll_CSSX(bool admin,bool cssx)
        ///    true : Tuyet Lan 
        ///    false : Mai Lan
        /// </summary>
        /// <param name="admin"></param>
        /// <returns></returns>
        public static XPCollection<Category> GetAll_CSSX(bool admin,bool cssx)
        {
            XPCollection<Category> _xpCategory = new XPCollection<Category>(KetNoiCSDL.session);
            if (admin == false)
                _xpCategory.Filter=DevExpress.Data.Filtering.CriteriaOperator.Parse("Active=true and CoSoSX==?",cssx);
            else
                _xpCategory.Filter = DevExpress.Data.Filtering.CriteriaOperator.Parse("CoSoSX==?", cssx);
            _xpCategory.Sorting = SetupSort(ColumCategory._Priority, true);
            return _xpCategory;
        }
        public static int DeleteCat(Category cat)
        {
            if (cat != null)
            {
                CategorySubDB.DeleteAllCatSubOfCat(cat.Id);
                KetNoiCSDL.session.Delete(cat);
                return 1;
            }
            return 0;
        }
    }
}
